package com.oneonefive.educationsystem.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oneonefive.educationsystem.domain.TeacherRequest;
import com.oneonefive.educationsystem.domain.User;
import com.oneonefive.educationsystem.dto.Result;
import com.oneonefive.educationsystem.mapper.TeacherRequestMapper;
import com.oneonefive.educationsystem.service.SemesterService;
import com.oneonefive.educationsystem.service.TeacherRequestService;
import com.oneonefive.educationsystem.utils.Constant;
import com.oneonefive.educationsystem.utils.StateChangeUtil;
import com.oneonefive.educationsystem.vo.SemesterVo;
import com.oneonefive.educationsystem.vo.TeacherRequestVo;
import io.netty.util.internal.StringUtil;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;


@Service
public class TeacherRequestServiceImpl extends ServiceImpl<TeacherRequestMapper, TeacherRequest>
    implements TeacherRequestService{

    @Resource
    TeacherRequestMapper teacherRequestMapper;

    @Resource
    SemesterService semesterService;

    @Override
    public Result teaReqList(TeacherRequestVo teacherRequestVo) {
        List<TeacherRequestVo> list;
        list=teacherRequestMapper.teaReqListCount(teacherRequestVo);
        long total = list.size();
        if (teacherRequestVo.getPage() != null) {
            teacherRequestVo.setPage((teacherRequestVo.getPage()-1)*10);
            list = teacherRequestMapper.teaReqList(teacherRequestVo);
        }
        List<TeacherRequestVo> resList = getResVo(list);
        return Result.ok(resList, total);
    }

    @Override
    public Result legitimacy(TeacherRequest teacherRequest) {
        if (!Pattern.matches(Constant.TERM_REGEX, teacherRequest.getTerm())) {
            return Result.fail("输入的学期不符合格式");
        }
        if (teacherRequest.getLessonName().length() > 20) {
            return Result.fail("输入的的课程名过长");
        }
        if (teacherRequest.getStuClass().length() > 20) {
            return Result.fail("输入的的班级名字过长");
        }
        if (teacherRequest.getLabType() < 0 || teacherRequest.getLabType() > 3) {
            return Result.fail("输入的实验室类型不存在");
        }
        if (teacherRequest.getStuNum() < 0) {
            return Result.fail("学生人数不能为负");
        }
        SemesterVo semesterVo = new SemesterVo();
        semesterVo.setTerm(teacherRequest.getTerm());
        Result result = semesterService.semesterList(semesterVo);
        semesterVo = ((List<SemesterVo>) result.getData()).get(0);
        if (result.getTotal() == 0) {
            return Result.fail("该学期不存在");
        }
        if (teacherRequest.getStartWeek() < 0 || teacherRequest.getStartWeek() > teacherRequest.getEndWeek()) {
            return Result.fail("输入的周次不合法");
        }
        if (teacherRequest.getEndWeek() > semesterVo.getWeek()) {
            return Result.fail("申请的结束周大于该学期的周数");
        }
        if (teacherRequest.getSection() > 5) {
            return Result.fail("输入的节次不存在");
        }

        return Result.ok();

    }

    @Override
    public Result setValue(TeacherRequest teacherRequest, User user) {
        if (StringUtil.isNullOrEmpty(teacherRequest.getTerm())) {
            if (Constant.SEMESTER == null) {
                return Result.fail("未设置当前学期");
            }
            teacherRequest.setTerm(Constant.SEMESTER.getTerm());
            System.out.println("设置当前学期");
        }
        if (teacherRequest.getUserId() == null) {
            teacherRequest.setUserId(user.getUserId());
        }
        return Result.ok("term和userid未丢失");
    }

    public List<TeacherRequestVo> getResVo(List<TeacherRequestVo> list) {
        List<TeacherRequestVo> resList = new ArrayList<>();
        for (TeacherRequest teacherRequest : list) {
            TeacherRequestVo teacherRequestVo = new TeacherRequestVo();
            BeanUtils.copyProperties(teacherRequest, teacherRequestVo);
            teacherRequestVo.setStateString(StateChangeUtil.teaReqStateChange(teacherRequest.getState()));
            teacherRequestVo.setSectionString(StateChangeUtil.sectionChange(teacherRequest.getSection()));
            teacherRequestVo.setLabTypeString(StateChangeUtil.labTypeChange(teacherRequest.getLabType()));
            resList.add(teacherRequestVo);
        }
        return resList;
    }


}




